﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATM_Machine
{
    public partial class View : Form
    {
        string Cardnum;
        Model model = new Model();
        public View()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (model.login(Cardnum, textBox2.Text))
            {
                MessageBox.Show("bejelentkezve!");
                button1.Visible = true;
                button2.Visible = true;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TakeCash tk = new TakeCash();
            tk.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetBalance gb = new GetBalance();
            gb.Show();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if ((sender as TextBox).TextLength > 3)
            {
                Cardnum += (sender as TextBox).Text;
                if ((sender as TextBox).Name == "textBox1")
                this.textBox3.Focus();
                if ((sender as TextBox).Name == "textBox3")
                this.textBox4.Focus(); 
                if ((sender as TextBox).Name == "textBox4")
                this.textBox5.Focus();
                if ((sender as TextBox).Name == "textBox4")
                    Cardnum += (sender as TextBox);
            }
        }
    }

    public class Model
    {
        static string cardName, PIN;
        bool logged=false;
       
        static Random r = new Random();
        static int cash = r.Next(5000, 20000);
       
        public Model()
        {
           
            
        }
        public bool login(string cardName,string PIN)
        {
            int i = cardName.Length - 1;
            int j = 3;
         while(cardName[i]==PIN[j])
            {
                if (j == 0)
                { logged = true;
                    return true; }
            i--;
            j--;
            }

         if ((cardName.Length > 16) || (j > 0) || (PIN.Length > 4))
         {
             logged = false; 
             return false; 
         }
         else
         {
             logged = true;
             return true;
         }
        }
        public void logout()
        {
           
        }
        public int getBalance()
        {
          
            return cash;
        }
        public bool takeCash(int amount)
        {
            if (amount < cash) { cash = cash - amount; return true; }
            else { return false; }
        }
    }
}
